#!/usr/bin/env python
# -*- coding: utf-8 -*-  
# Copyright (c)  2011 flykeysky<at>gmail.com. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 2 of the License, or
# version 3 of the License, or (at your option) any later version. It is
# provided for educational purposes and is distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.

import os, sys
from PyQt4 import QtCore, QtGui
from configwizardui import Ui_ConfigWizard

import sigutils

GlobalConfig = {'ARFFFileName_s':'',
                'InputPayloadPath_s':'',
                'OutputSigDir_s':''
                }
CONFIGFileName = 'config.yaml'

class StartConfigWizard(QtGui.QWizard):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_ConfigWizard()
        self.ui.setupUi(self)
        self.user_dir = QtCore.QDir.homePath()
        #self.DisplayInit()
        self.connect(self.ui.pushButton_ARFF,QtCore.SIGNAL("clicked()"), self.loadARFF)
        self.connect(self.ui.pushButton_PayloadPath,QtCore.SIGNAL("clicked()"), self.setPayloadPath)
        self.connect(self.ui.pushButton_OutputPath,QtCore.SIGNAL("clicked()"), self.setOutputPath)
        global GlobalConfig, CONFIGFileName
        GlobalConfig = sigutils.LoadYAMLStream(CONFIGFileName)
        self.ui.lineEdit_ARFF.setText(GlobalConfig['ARFFFileName_s'])
        self.ui.lineEdit_PayloadPath.setText(GlobalConfig['InputPayloadPath_s'])
        self.ui.lineEdit_OutputPath.setText(GlobalConfig['OutputSigDir_s'])
        #self.connect(self.ui.pushButton_ARFF,QtCore.SIGNAL("valueChanged(QString)"), self.ForLineEditorsChanged)
    def loadARFF(self):
        #print("load ARFF...")
        fd = QtGui.QFileDialog(self)
        GlobalConfig['ARFFFileName_s'] = fd.getOpenFileName( self,"Open ARFF file", self.user_dir, "arff files (*.arff *.ARFF *.txt)" )
        self.ui.lineEdit_ARFF.setText(GlobalConfig['ARFFFileName_s'])
    def setPayloadPath(self):
        fd = QtGui.QFileDialog(self)
        GlobalConfig['InputPayloadPath_s'] = fd.getExistingDirectory(self, "Open Directory", \
                                                                      self.user_dir,\
                                                                      QtGui.QFileDialog.ShowDirsOnly|QtGui.QFileDialog.DontResolveSymlinks)
        self.ui.lineEdit_PayloadPath.setText(GlobalConfig['InputPayloadPath_s'])
    def setOutputPath(self):
        fd = QtGui.QFileDialog(self)
        GlobalConfig['OutputSigDir_s'] = fd.getExistingDirectory(self, "Open Directory", \
                                                                      self.user_dir,\
                                                                      QtGui.QFileDialog.ShowDirsOnly|QtGui.QFileDialog.DontResolveSymlinks)
        self.ui.lineEdit_OutputPath.setText(GlobalConfig['OutputSigDir_s'])
    def ForLineEditorsChanged(self):
        GlobalConfig['ARFFFileName'] = self.ui.lineEdit_ARFF.displayText()
        print(GlobalConfig['ARFFFileName'])
    
    def accept(self):
        GlobalConfig['ARFFFileName'] = self.ui.lineEdit_ARFF.displayText()
        GlobalConfig['InputPayloadPath_s'] =  self.ui.lineEdit_PayloadPath.displayText()
        GlobalConfig['OutputSigDir_s'] =  self.ui.lineEdit_OutputPath.displayText()
        print( "%s\t%s\t%s" % \
               (GlobalConfig['ARFFFileName'],\
                GlobalConfig['InputPayloadPath_s'],\
                GlobalConfig['OutputSigDir_s']) )
        sigutils.SaveYAMLStream( GlobalConfig, CONFIGFileName)
        self.hide()
        super(StartConfigWizard, self).accept()
        
def main():
    app = QtGui.QApplication(sys.argv)
    myapp = StartConfigWizard()
    myapp.show()
    sys.exit(app.exec_())
    print ("done!")

# Process command-line arguments.
if __name__ == '__main__':
    main()
    exit()

